<?php
/**
 * 管理文章
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('InfoModel', 'info');
class AdminController extends UE_Controller_Admin {	
	/**
	 * @var InfoModel
	 */
	public $infoModel ;
	public function init() {
		$this->_cats = CatModel::getCats('info', 0, false);
		$this->infoModel = new InfoModel();
	}
	
	public function addAction() {	    
	    if (IS_POST) {
	    	if ($this->infoModel->add($_POST)) {
	    		$url = InfoModel::getInfoUrl($_POST['info_id'], $_POST['cat_id']);
	    		$editUrl = UE_Url::make('info/admin/edit/'.$_POST['info_id']);
	    		$this->setOK('成功添加文章（<a href="'. $url.'" target="_blank">查看</a>, <a href="'. $editUrl.'" target="_blank">查看</a>）');
	    	} else {
	    		$this->setErr($this->infoModel->getErrs());
	    	}
	    }
	    
	    include $this->tpl();
	}
	
	public function listAction() {		
		if (IS_POST) {
			//print_r($_POST);
			if (!empty($_POST['optype'])) {
				$kids = '';
				if(!empty($_POST['list-check'])) {
					$keys = array_keys($_POST['list-check']);
					$kids = join(',', $keys);
				}
				
				switch ($_POST['optype']) {
					// 排序
					case 'sort':
						foreach ($_POST['cat_order'] as $infoId => $infoOrder) {
							UE_DB::factory()->query("UPDATE ue_info SET info_order='$infoOrder' WHERE info_id='$infoId' LIMIT 1");
						}
						break;
					// 放进回收站
					case 'trash':					
						$kids && UE_DB::factory()->query("UPDATE ue_info SET info_status='-1' WHERE info_id IN($kids)");
						break;
					// 删除
					case 'del':
						$kids && UE_DB::factory()->query("DELETE FROM ue_info WHERE info_id IN($kids)");
						break;
					// 设为发布/启用/通过审核
					case 'pub':
						$kids && UE_DB::factory()->query("UPDATE ue_info SET info_status='1' WHERE info_id IN($kids)");
						break;
					// 设为草稿/未审核
					case 'draft':
						$kids && UE_DB::factory()->query("UPDATE ue_info SET info_status='0' WHERE info_id IN($kids)");
						break;						
				}
			}
		}
		
		$filter = '';
		// 状态
		if (isset($_GET['status'])) {
			$filter .= " AND info_status='{$_GET['status']}' ";
		} else if($this->id != 'all') {
		    $filter .= " AND info_status >= 0 ";
		}
		
		if (!empty($_GET['keyword'])) {
			$filter .= " AND info_title LIKE '%{$_GET['keyword']}%'";
		}
		//print_r($_GET);
		// 栏目
		if (isset($_GET['cat_id']) && $_GET['cat_id'] != 'all') {
			if (empty($_GET['cat_id'])) {
				$filter .= " AND cat_id = '0' ";
			} else {
				if(!empty($this->_cats[$_GET['cat_id']]['chileId'])) {
				    $filter .= " AND cat_id IN (".$this->_cats[$_GET['cat_id']]['chileId'].") ";
				} else {
					$filter .= " AND cat_id = '{$_GET['cat_id']}' ";
				}
			}			
		}
		
		$list = array();
		$paging = '';
		$totals = UE_DB::factory()->getfirstField("SELECT COUNT(*) FROM ue_info WHERE true $filter");
		if ($totals) {
			$p = new UE_Paging();
			$p->setVar($totals, 20);
			$paging = $p->pagingStr;
			$sql = "SELECT * FROM ue_info 
			        WHERE true $filter 
			        ORDER BY info_id DESC
			        LIMIT {$p->firstCount}, {$p->pageNo}";
			$list = UE_DB::factory()->getAll($sql);
		}
		
	    include $this->tpl();
	}
	
	public function editAction() {
		if (!$this->id) {
			print 'Error Param!';
			return ;
		}
		
		$this->infoModel->setId($this->id);
		
		if (IS_POST) {
			if($this->infoModel->update($_POST)) {
				$url = InfoModel::getInfoUrl($this->id, $_POST['cat_id']);
				$this->setOK('成功编辑文章（<a href="'. $url.'" target="_blank">查看</a>）');
			} else {
				$this->setErr($this->infoModel->getErrs());
			}
		}
		
		$infoItem = $this->infoModel->read();
		//print_r($infoItem);

		include $this->tpl(); 
	}
	
	public function delAction() {
		if (empty($this->id)) {
			die('Error Param');
		}
		
		if($this->infoModel->setId((int)$this->id)->delete()) {
			header('Location: '. UE::referer());
		} else {
			$this->setErr($this->infoModel->getErrs());
			$this->showMessage();
		}	    
	}	
}